package com.job.config;

import com.job.common.ResponseResult;
import com.job.common.enums.ResultCodeEnum;
import com.job.common.exception.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public ResponseResult<ResultCodeEnum> nullPointerExceptionHandler(NullPointerException exception) {
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.FAIL);
    }

    @ResponseBody
    @ExceptionHandler(ParameterIsNullException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(ParameterIsNullException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.PARAMETER_IS_NULL_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(CityIsNullException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(CityIsNullException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.CITY_IS_NULL_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(AddCorporationException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(AddCorporationException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.ADD_CORPORATION_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(BadCredentialsException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(BadCredentialsException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
    }

    @ResponseBody
    @ExceptionHandler(AvatarUpdateException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(AvatarUpdateException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.AVATAR_UPDATE_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(RegisterException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(RegisterException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.REGISTER_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(UserUpdateException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(UserUpdateException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.CORPORATION_INFO_BANDING_FAIL);
    }

    @ResponseBody
    @ExceptionHandler(UserException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(UserException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.USER_IS_NULL);
    }

    @ResponseBody
    @ExceptionHandler(LoginException.class)
    public ResponseResult<ResultCodeEnum> serviceInvokeExceptionHandler(LoginException exception){
        log.error(exception.getMessage());
        return new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
    }



}
